package com.liuyk.commonlib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.liuyk.commonlib.R;

/**
 * 标题栏
 * <p>
 * Created by liuyakui on 2020/10/22.
 */
public class TitleBar extends LinearLayout implements View.OnClickListener {
    private ViewGroup mToolbar;
    private ImageView mTitleBarLeft;
    private TextView mTitleBarTitle;
    private TextView mTitleBarRight;
    private ImageView mTitleBarRightIcon;
    private boolean isShowToolBar;

    private TitleClickListener mTitleClickListener;

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(attrs);
    }

    private void initView(AttributeSet attributeSet) {
        LayoutInflater.from(getContext()).inflate(R.layout.base_title_bar, this);
        mToolbar = findViewById(R.id.tool_bar);

        mTitleBarLeft = findViewById(R.id.title_bar_left);
        mTitleBarTitle = findViewById(R.id.title_bar_title);
        mTitleBarRight = findViewById(R.id.title_bar_right);
        mTitleBarRightIcon = findViewById(R.id.title_bar_right_icon);
        mTitleBarRightIcon.setOnClickListener(this);
        mTitleBarLeft.setOnClickListener(this);
        mTitleBarRight.setOnClickListener(this);

        TypedArray ta = getContext().obtainStyledAttributes(attributeSet, R.styleable.TitleBar);

        boolean isHide = ta.getBoolean(R.styleable.TitleBar_title_bar_is_hide, false);
        if (isHide) {
            hideTitleBar();
        } else {
            showTitleBar();
        }

        int titleTextColor = ta.getColor(R.styleable.TitleBar_title_bar_title_text_color,
                Color.parseColor("#333333"));
        setTitleBarTextColor(titleTextColor);
        String titleText = ta.getString(R.styleable.TitleBar_title_bar_title_text);
        setTitleBarTitle(titleText);
        float titleTextSize = ta.getDimension(R.styleable.TitleBar_title_bar_title_text_size, 17);
        setTitleBarTextSize(titleTextSize);

        int titleTextColorR = ta.getColor(R.styleable.TitleBar_title_bar_right_text_color,
                Color.parseColor("#333333"));
        setTitleBarRightColor(titleTextColorR);
        String titleTextR = ta.getString(R.styleable.TitleBar_title_bar_right_text);
        setTitleBarRightText(titleTextR);
        float titleTextSizeR = ta.getDimension(R.styleable.TitleBar_title_bar_right_text_size, 17);
        setTitleBarRightTextSize(titleTextSizeR);

        Drawable ld = ta.getDrawable(R.styleable.TitleBar_title_bar_left_icon);
        if (ld != null) {
            setTitleBarLeftIcon(ld);
        }
        Drawable rd = ta.getDrawable(R.styleable.TitleBar_title_bar_right_icon);
        if (ld != null) {
            setTitleBarRightIcon(rd);
        }

        ta.recycle();
    }

    public void setTitleBarLeftIcon(int resId) {
        if (mTitleBarLeft == null) {
            return;
        }
        if (resId != -1) {
            mTitleBarLeft.setImageResource(resId);
        } else {
            mTitleBarLeft.setVisibility(View.GONE);
        }
    }

    public void setTitleBarLeftIcon(Drawable drawable) {
        if (mTitleBarLeft == null) {
            return;
        }
        if (drawable != null) {
            mTitleBarLeft.setImageDrawable(drawable);
        } else {
            mTitleBarLeft.setVisibility(View.GONE);
        }
    }

    public void setTitleBarTitle(String title) {
        mTitleBarTitle.setText(title);
    }

    public void setTitleBarTitle(int resId) {
        setTitleBarTitle(getContext().getString(resId));
    }

    public void setTitleBarTextColor(int color) {
        mTitleBarTitle.setTextColor(color);
    }

    public void setTitleBarTextSize(float size) {
        mTitleBarTitle.setTextSize(size);
    }

    public void setTitleBarRightTextSize(float size) {
        mTitleBarRight.setTextSize(size);
    }

    public void setTitleBarRightText(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            mTitleBarRight.setVisibility(View.VISIBLE);
            mTitleBarRight.setText(msg);
        } else {
            mTitleBarRight.setVisibility(View.GONE);
        }
    }

    public void setTitleBarRightText(int resId) {
        setTitleBarRightText(getContext().getString(resId));
    }

    public void setTitleBarRightColor(int resId) {
        mTitleBarRight.setTextColor(resId);
    }

    public void setTitleBarRightIcon(Drawable drawable) {
        if (drawable != null) {
            mTitleBarRightIcon.setImageDrawable(drawable);
        } else {
            mTitleBarRightIcon.setVisibility(View.GONE);
        }
    }

    public void setTitleBarRightIcon(int resId) {
        if (resId != -1) {
            mTitleBarRightIcon.setImageResource(resId);
        } else {
            mTitleBarRightIcon.setVisibility(View.GONE);
        }
    }

    public void hideTitleBar() {
        setVisibility(View.GONE);
    }

    public void showTitleBar() {
        setVisibility(View.VISIBLE);
    }

    public boolean isShowToolBar() {
        return isShowToolBar;
    }

    public void setShowToolBar(boolean showToolBar) {
        isShowToolBar = showToolBar;
        if (isShowToolBar) {
            setVisibility(View.VISIBLE);
        } else {
            setVisibility(View.GONE);
        }
    }

    public void setTitleClickListener(TitleClickListener titleClickListener) {
        mTitleClickListener = titleClickListener;
    }

    public interface TitleClickListener {
        //标题栏返回键
        void titleBarLeftClick(View v);

        //标题栏右边文字 点击事件
        void titleBarRightTextClick(View v);

        //标题栏右边icon 点击事件
        void titleBarRightIconClick(View v);
    }

    //亮色title
    public void lightTitleBar() {
        setBackgroundColor(Color.WHITE);
        setTitleBarLeftIcon(R.mipmap.back_black);
        setTitleBarTextColor(Color.parseColor("#333333"));
        setTitleBarRightColor(Color.parseColor("#333333"));
    }

    //暗色title
    public void darkTitleBar() {
        setBackgroundColor(Color.BLACK);
        setTitleBarLeftIcon(R.mipmap.back_white);
        setTitleBarTextColor(Color.WHITE);
        setTitleBarRightColor(Color.WHITE);
    }

    public void fullscreenImmersion(int bg) {
        darkTitleBar();
        setBackgroundResource(bg);
    }

    @Override
    public void onClick(View v) {
        if (v == mTitleBarLeft) {
            if (mTitleClickListener != null) {
                mTitleClickListener.titleBarLeftClick(v);
            }
        }
        if (v == mTitleBarRight) {
            if (mTitleClickListener != null) {
                mTitleClickListener.titleBarRightTextClick(v);
            }
        }
        if (v == mTitleBarRightIcon) {
            if (mTitleClickListener != null) {
                mTitleClickListener.titleBarRightIconClick(v);
            }
        }
    }
}